package com.banzhi.shop.entity.InviteShareSuCaimatterInfo;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by JIANGFEI on 2018/4/19.
 */

public class InviteShareSuCaiImpl implements IInviteShareSuCaiModel {

    @Override
    public void getData(String goods_id, final InviteShareSuCaiCallBack callBack ) {
        RequestParams params = new RequestParams( URLString.hostUrl + URLString.inviteShopKeeperSuCaiURL);
        params.addBodyParameter("goods_id", goods_id);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.e("InviteShareSuCaiImpl", "---params----" + params.toString());
        x.http().post( params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject( result );
                    if (jsonObject.optString( "code" ).equals( "0" )) {
                        Gson gson = new Gson();
                        InviteShareSuCai inviteShareSuCai = gson.fromJson( result, InviteShareSuCai.class );
                        callBack.ongetInviteShareSuCaiDataSuccess( inviteShareSuCai.getRes());

                    }else {
                        callBack.ongetInviteShareSuCaiDataError( jsonObject );
                    }




                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                LogUtils.e("getSucaiData", "---onError----" +  throwable.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(86)");
            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {
                //LoadingDialogUtils.dismiss();

            }
        } );

    }
}
